package com.shy.configs.mvc.configuration;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.shy.collect.mapper.DicPayTypeMapper;
import com.shy.collect.mapper.DicPayWayMapper;
import com.shy.configs.mapping.GroupConcat;
import com.shy.configs.mvc.models.vo.DicItemVo;
import jakarta.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class BossInitConfig implements CommandLineRunner {

    @Resource
    private ApplicationContext ctx;

    public static List<DicItemVo> PAY_WAY_DIC_LIST = new ArrayList<>();

    public static Map<String, List<DicItemVo>> PAY_TYPE_DIC_MAP = new HashMap<>();


    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    //声明线程池
    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        return new ThreadPoolExecutor(33, 44, 3, TimeUnit.MINUTES, new LinkedBlockingDeque<>());
    }


    @Override
    public void run(String... args) throws Exception {
        //读取字典表的数据， 初始化支付方式数据
        DicPayWayMapper dicPayWayMapper = ctx.getBean(DicPayWayMapper.class);
        PAY_WAY_DIC_LIST = dicPayWayMapper.selectDicList();

        //查询支付类型
        DicPayTypeMapper dicPayTypeMapper = ctx.getBean(DicPayTypeMapper.class);
        List<GroupConcat> groupConcatList = dicPayTypeMapper.selectDicList();

        for (GroupConcat groupConcat : groupConcatList) {

            List<DicItemVo> dicItemVoList = Arrays.stream(groupConcat.getContent().split("#"))
                    .map(str -> {
                                String[] content = str.split("=");
                                return new DicItemVo(content[0], content[1]);
                            }
                    ).toList();

            PAY_TYPE_DIC_MAP.put(groupConcat.getWayCode(), dicItemVoList);
        }

    }
}
